#include "paint_image.h"
#include <QMainWindow>

QColor PaintImage::myPenColor = Qt::blue;
void PaintImage::setPenWidth(int newWidth)
{
	this->myPenWidth = newWidth;

}


void PaintImage::setPenColor(const QColor &newColor)

{
	PaintImage::myPenColor = newColor;

}

void PaintImage::applyFilter(Filter* filter)
{
	this->image = filter->apply(image);

}

void PaintImage::clearImage()
{

}

void PaintImage::print()
{


}



 PaintImage::PaintImage(QWidget *parent)
     : QWidget(parent)
 {
     setAttribute(Qt::WA_StaticContents);
     modified = false;
     scribbling = false;
     myPenWidth = 1;
     
 }

void  PaintImage::mousePressEvent(QMouseEvent *event)
 {
     if (event->button() == Qt::LeftButton) {
		 
		 if(getCurrentTool() == "PaintBrush")
		 {
			 lastPoint = event->pos();
			 scribbling = true;
		 }

		if (getCurrentTool() == "EyedropperTool")
		{
			lastPoint = event ->pos();                      
			QRgb pick = getImage().pixel(this ->lastPoint);
			QColor color(pick);
			PaintImage::myPenColor = color;
		}

		 
     }
 }

const QString PaintImage::getCurrentTool()
{
	 const QWidget* mainWindow = this->parentWidget()->parentWidget()->parentWidget();
	 return  mainWindow->findChild<QLabel*>()->text();
}
void  PaintImage::mouseMoveEvent(QMouseEvent *event)
 {
     if ((event->buttons() & Qt::LeftButton) && scribbling)
         drawLineTo(event->pos());
 }
void  PaintImage::mouseReleaseEvent(QMouseEvent *event)
 {
     if (event->button() == Qt::LeftButton && scribbling) {
         drawLineTo(event->pos());
         scribbling = false;
     }
 }

void  PaintImage::paintEvent(QPaintEvent *event)
 {
     QPainter painter(this);
     QRect dirtyRect = event->rect();
     painter.drawImage(dirtyRect, image, dirtyRect);
 }

void  PaintImage::resizeEvent(QResizeEvent *event)
{
}

void PaintImage::openImage(QImage& newImage)
{
	QSize newSize = newImage.size().expandedTo(size());
	resizeImage(&newImage, newSize);
	image=newImage;
	modified = false;
    update();


}

bool PaintImage::openImage(const QString &fileName)
 {
     QImage loadedImage;
     if (!loadedImage.load(fileName))
         return false;

     QSize newSize = loadedImage.size().expandedTo(size());
     resizeImage(&loadedImage, newSize);
     image = loadedImage;
     modified = false;
     update();
     return true;
 }

void PaintImage::resizeImage(QImage *image, const QSize &newSize)
 {
     if (image->size() == newSize)
         return;

     QImage newImage(newSize, QImage::Format_RGB32);
     newImage.fill(qRgb(255, 255, 255));
     QPainter painter(&newImage);
     painter.drawImage(QPoint(0, 0), *image);
     *image = newImage;
 }

void PaintImage::drawLineTo(const QPoint &endPoint)
 {
     QPainter painter(&image);
     painter.setPen(QPen(PaintImage::myPenColor, myPenWidth, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
     painter.drawLine(lastPoint, endPoint);
     modified = true;

     int rad = (myPenWidth / 2) + 2;
     update(QRect(lastPoint, endPoint).normalized().adjusted(-rad, -rad, +rad, +rad));
     lastPoint = endPoint;
 }


 bool PaintImage::saveImage(const QString &fileName, const char *fileFormat)
 {
     QImage visibleImage = image;
     //resizeImage(&visibleImage, size());

     if (visibleImage.save(fileName, fileFormat)) {
         modified = false;
         return true;
     } else {
         return false;
     }
 }